# =RJabberD - Jabber Server for Ruby
# License:: Ruby's license (see LICENSE file), GNU GPL or GNU LGPL at your option.
# Website:: http://code.google.com/p/rjabberd

module RJabberD
  class Connection
    attr_accessor :sock

    def initialize(sock, logger)
      @sock = sock
      @logger = logger
      @parser = StreamParser.new(sock, self)
    end

    def start
      begin
        @parser.parse
      rescue IOError
        # Do nothing, the client was disconnected.
      end
    end
    
    # The start of an element was encountered
    def parse_start(element)
      @logger.info(element.to_s)
    end

    # The end of an element was encountered
    def parse_end(element)
      @logger.end("END #{element.to_s}")
    end

    # The end of the stream was encountered
    def parse_close
      @parser.terminate
    end

    # An exception occured.
    def parse_failure(exception)
      @logger.error(exception) 
      # As per XMPP recommendation.
      @parser.terminate
    end
  end
end
